package com.ithaima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ithaima.reggie.common.CustomException;
import com.ithaima.reggie.entity.Category;
import com.ithaima.reggie.entity.Dish;
import com.ithaima.reggie.entity.Setmeal;
import com.ithaima.reggie.mapper.CategoryMapper;
import com.ithaima.reggie.service.CategoryService;
import com.ithaima.reggie.service.DishService;
import com.ithaima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;


    /**
     * 根据id删除分类，删除之前需要进行判断
     * @param id
     */
    @Override
    public void remove(Long id) {
        //添加查询条件，根据分类id进行查询菜品数据
        long count = dishService.count(new LambdaQueryWrapper<Dish>().eq(Dish::getCategoryId, id));
        if (count > 0){
            //当前分类下有菜品，不能删除
            throw new CustomException("当前分类下有菜品，不能删除");
        }
        //添加查询条件，根据分类id进行查询套餐数据
        long count2 = setmealService.count(new LambdaQueryWrapper<Setmeal>().eq(Setmeal::getCategoryId, id));
        if (count2 > 0){
            //当前分类下有套餐，不能删除
            throw new CustomException("当前分类下有套餐，不能删除");
        }
        super.removeById(id);
    }
}
